<?php
class UserController extends FrontController
{
	public function loadAction()
	{
		$tbUser = new Table_User();
		
		$roleId = $this->getRequest()->roleId;
		$search = $this->getRequest()->search;
		$start = $this->getRequest()->start;
		$limit = $this->getRequest()->limit;
		
		if(is_numeric($roleId))
			return $tbUser->getList($roleId, $start, $limit);
		else
			return $tbUser->search($search['field'], $search['value'], $start, $limit);
	}
	
	public function addAction()
	{
		$tbUser = new Table_User();
		
		$data = $this->getRequest()
			->getCollection('role_id', 'email', 'password', 'first_name', 'last_name', 'active');
		$data['password'] = md5($data['password']);
		$data['active'] = $data['active']? 1 : 0;
		$data['register_time'] = time();
		
		$id = $tbUser->insert($data);
		return array('success' => true, 'data' => array('id' => $id));
	}
	
	public function updateAction()
	{
		$tbUser = new Table_User();
		$request = $this->getRequest();
		$id = $request->id;
		$data = array(
			$request->field => $request->value
		);
		
		$row = $tbUser->updateById($id, $data);
		return array('success' => is_numeric($row));
	}
	
	public function delAction()
	{
		$tbUser = new Table_User();
		$ids = $this->getRequest()->ids;
		
		$tbUser->deleteById($ids);
		return array('success' => true);
	}
}